package com.waitingsnow.modules.shell.view.giftItem
{
import assets.shellView.GiftBgAsset;

import com.waitingsnow.core.display.control.ToolTipManager;
import com.waitingsnow.core.model.vo.GiftVO;

import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.clearInterval;
import flash.utils.setInterval;

/**
 * 
 * @author waitingsnow
 * $Id: SpecialGiftItem.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class SpecialGiftItem extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>SpecialGiftItem</code> instance.
	 * 
	 */
	public function SpecialGiftItem()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var imgLoader:Loader;
	private var giftBgAsset:GiftBgAsset;
	private var bgShape:Shape;
	private var _itemWidth:int = 26;
	private var _itemHeight:int = 26;
	/**
	 * 倒计时剩余时间
	 */	
	private var _leftTime:Number;
	private var intervalID:int;
	public var totalTime:int = 10;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	private var _giftVO:GiftVO;
	public function get giftVO():GiftVO
	{
		return _giftVO;
	}
	
	public function get isReady():Boolean
	{
		return _leftTime <= 0;
	}
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		giftBgAsset = new GiftBgAsset();
		giftBgAsset.width = giftBgAsset.height = _itemWidth;
		addChildAt(giftBgAsset, 0);
	}
	
	public function countDownGift():void
	{
		_leftTime = totalTime;
		if (!bgShape)
		{
			bgShape = new Shape();
			bgShape.x = bgShape.y = _itemWidth/2;
			addChild(bgShape);
		}
		bgShape.visible = _leftTime > 0;
		clearInterval(intervalID);
		reDraw();
		intervalID = setInterval(reDraw, 100);
	}
	
	public function isEnd():void
	{
		_leftTime = 0;
	}
	
	private function reDraw():void
	{
		_leftTime -= 0.1;
		bgShape.visible = _leftTime > 0;
		if (_leftTime > 0)
		{
			var startAngel:int = int(360*(1-_leftTime/totalTime) - 90);
			drawSector(bgShape, _itemWidth/2 + 3, startAngel, 270);
			ToolTipManager.getInstance().addToolTip(this, _giftVO.giftName + 
				"正在累积中，还需" + int(_leftTime) + "秒");
			giftBgAsset.mouseEnabled = buttonMode = false;
		}
		else
		{
			clearInterval(intervalID);
			ToolTipManager.getInstance().addToolTip(this, 
				"您已累积1个" + _giftVO.giftName + "，单击送给歌手");
			giftBgAsset.mouseEnabled = buttonMode = true;
		}
	}
	
	private function drawSector(disObj:Shape, 
								radius:int,
								startAngel:int, 
								endAngel:int, 
								bgColor:uint = 0x696969, 
								bgAlpha:Number = 0.7,
								lineColor:uint = 0x717171):void
	{
		var i:int;
		with (disObj)
		{
			graphics.clear();
			lineColor && graphics.lineStyle(null, lineColor);
			graphics.beginFill(bgColor, bgAlpha);
			graphics.moveTo(0, 0);
			for (i = startAngel; i <= endAngel; i++)
			{
				graphics.lineTo(radius * Math.cos(i * Math.PI / 180), 
					radius * Math.sin(i * Math.PI / 180));
			}
			graphics.endFill();
			graphics.lineTo(0, 0);
		}
	}
	
	public function setData(vo:GiftVO):void
	{
		_giftVO = vo;
		if (!imgLoader)
		{
			imgLoader = new Loader();
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaderCompleteHandler);
		}
		vo.iconPath && imgLoader.load(new URLRequest(vo.iconPath));
		//倒计时
		countDownGift();
	}
	
	public function setSize(width:int, height:int):void
	{
		_itemWidth = width;
		_itemHeight = height;
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function imgLoaderCompleteHandler(event:Event):void
	{
		/*imgLoader.width = _itemWidth;
		imgLoader.height = _itemHeight;*/
		imgLoader.mouseChildren = imgLoader.mouseEnabled = false;
		addChildAt(imgLoader, 1);
	}
}
}